package com.zzzzzz.oms.shipment;

import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import com.zzzzzz.plugins.jdbctemplate.BaseDao;
import com.zzzzzz.plugins.jdbctemplate.Finder;
import com.zzzzzz.plugins.jdbctemplate.DaoUtils;
import com.zzzzzz.utils.page.Page;
import com.zzzzzz.plugins.shiro.I;

/**
 * @author hing
 * @version 1.0.0
 */
@Repository
public class ShipmentDao {
	
	@Resource
	private BaseDao baseDao;	
	
	//SQL 
	private final static String sql_add = "insert into t_shipment(cd, platformId, constractorId, ftranslocationId, ttranslocationId, status, vehicleId, vehicleTypeId, carNo, driverId, driverName, phone, idcard, points, shipment_method, vocationType, gzNo,expectMoney, quantity, weight, volume, palletsum, expense, descr, planltime, planatime, addDt, addBy) values(:cd, :platformId, :constractorId, :ftranslocationId, :ttranslocationId, :status, :vehicleId, :vehicleTypeId, :carNo, :driverId, :driverName, :phone, :idcard, :points, :shipment_method, :vocationType, :gzNo, :expectMoney, :quantity, :weight, :volume, :palletsum, :expense, :descr, :planltime, :planatime, :addDt, :addBy)";
	private final static String sql_upd = "update t_shipment set cd=:cd, constractorId=:constractorId,ftranslocationId=:ftranslocationId,ttranslocationId=:ttranslocationId, status=:status, vehicleId=:vehicleId, vehicleTypeId=:vehicleTypeId,carNo=:carNo, driverId=:driverId, driverName=:driverName, phone=:phone, idcard=:idcard,points=:points,shipment_method=:shipment_method, vocationType=:vocationType,gzNo=:gzNo,expectMoney=:expectMoney, quantity=:quantity, weight=:weight, volume=:volume, palletsum=:palletsum, expense=:expense, descr=:descr, planltime=:planltime,planatime=:planatime, leavetime=:leavetime, arrivetime=:arrivetime, updDt=:updDt, updBy=:updBy where id = :id";
	private final static String sql_findById = "select id, cd, constractorId, ftranslocationId, ttranslocationId, status, vehicleId, vehicleTypeId,carNo, driverId, driverName, phone, idcard, points, shipment_method, vocationType, gzNo, expectMoney, quantity, weight, volume, palletsum, expense, descr, planltime, planatime, leavetime, arrivetime from t_shipment where id = :id";
	private final static String sql_upds="update t_shipment set quantity = :quantity,weight = :weight,volume = :volume,palletsum=:palletsum,points=:points, updDt = :updDt, updBy = :updBy where id =:id";
	private final static String sql_updstatusByIds="update t_shipment set status=:status, updDt = :updDt, updBy = :updBy where id in (:ids)";
	private final static String sql_updExpenseById="update t_shipment set expense=:expense,updDt = :updDt, updBy = :updBy where id=:id";
	
	// 增加
	public Long add(Shipment shipment){
		return baseDao.updateGetLongKey(sql_add, shipment);
	}
	
	// 修改调度单状态，主要是用于调度单的跟踪调度单状态的改变
	public int updStatusByIds(List<Long> ids, String status, I i){
		Finder finder = new Finder(sql_updstatusByIds).setParam("ids", ids).setParam("status", status)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}	
	
	// 修改调度单的一些基础数据，主要是用于调度单的增加分段订单，获取取消分段订单
	public int upds(Long id,Double quantity,Double weight,Double volume,Double palletsum,Integer points, I i){
		Finder finder = new Finder(sql_upds).setParam("id", id).setParam("quantity", quantity).setParam("weight", weight).setParam("volume", volume).setParam("palletsum", palletsum).setParam("points", points)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	
	// 修改数据
	public int updById(Shipment shipment){
		return baseDao.update(sql_upd, shipment);
	}
	
	// 查找一条数据
	public Shipment findById(Long id){
		Finder finder = new Finder(sql_findById).setParam("id", id);
		return baseDao.findOne(finder, Shipment.class);
	}
	
	// 更新一口价
	public int updExpenseById(Double expense,Long id,I i){
		Finder finder = new Finder(sql_updExpenseById).setParam("expense", expense).setParam("id",id)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}
	
	// 更新调度单的出发和运抵时间，主要是用于调度单跟踪的发车和运抵确认
	public int updTime(String time, String status,List<Long> shipmentIds, I i){
		Finder finder=new Finder("");
		finder.append("update t_shipment set");
		if(status.equals("onway")){
			finder.append("leavetime=:leavetime").setParam("leavetime", time);
		}else if(status.equals("arrive")){
			finder.append("arrivetime=:arrivetime").setParam("arrivetime", time);
		}
		finder.append("where id in (:ids)").setParam("ids", shipmentIds);
		return baseDao.update(finder);
	}
	
	/**
	 * 动态查询
	 */
	public List<Shipment> findListBy(Page page,Map<String, Object> ffMap,I i){
		Finder finder = new Finder("");
		finder.append(" select");
		finder.append("count(1) as tt");
		finder.append("from t_shipment s inner join t_constractor c on c.id=s.constractorId inner join sys_dict dv on dv.descr=s.vocationType inner join t_vehicle v on v.id=s.vehicleId inner join t_vehicleType vt on vt.id=s.vehicleTypeId "
				+ "inner join t_trans_location ft on ft.id=s.ftranslocationId inner join t_trans_location tt on tt.id=s.ttranslocationId left join sys_dict d on d.descr=s.shipment_method inner join sys_dict ss on s.status=ss.descr");				
		finder.append(" where 1=1");
		finder.append("and s.platformId=:platformId").setParam("platformId", i.getPlatformId());
		if(DaoUtils.isNotNull(ffMap.get("cd"))){
			finder.append(" and s.cd = :cd").setParam("cd", ffMap.get("cd"));
		}
		if(DaoUtils.isNotNull(ffMap.get("name"))){
			finder.append(" and s.name like :name").setParam("name", "%" + ffMap.get("name") + "%");
		}
		if(DaoUtils.isNotNull(ffMap.get("status"))){
			finder.append(" and s.status=:status").setParam("status",ffMap.get("status"));
		}
		if(DaoUtils.isNotNull(ffMap.get("driverId"))){
			finder.append(" and s.driverId=:driverId").setParam("driverId",ffMap.get("driverId"));
		}	
		if(DaoUtils.isNotNull(ffMap.get("carNo"))){
			finder.append(" and s.carNo =:carNo").setParam("carNo",ffMap.get("carNo"));
		}
		finder.append("order by s.id desc");
		if (page != null) {
			Integer total = baseDao.findInt(finder);
			page.setTotal(total);
			finder.limit(page.getStart(), page.getOffset());
		}
		String sql = finder.getSql().replace("count(1) as tt","s.id, s.constractorId, s.cd, s.carNo, s.driverName, s.phone, s.idcard, s.points, s.shipment_method, s.vocationType, s.gzNo, s.expectMoney, s.quantity, s.weight, s.volume, s.palletsum, s.expense, s.descr, s.planltime, s.planatime, s.leavetime, s.arrivetime,"
				+ "s.status, c.name as constractorName, dv.val as vocationTypeName, ft.name as ftranslocationName, tt.name as ttranslocationName, d.val as shipment_methodName, ss.val as statusName, vt.name as vehicleTypeName");
		finder.setSql(sql);
		List<Shipment> list = baseDao.findList(finder, Shipment.class);				
		return list;
	}
}
